<template>
    <div>
        <el-form-item label="用户名">
            <el-input v-model="username" />
        </el-form-item>
        <el-form-item label="密码">
            <el-input v-model="password" />
        </el-form-item>
        <el-button type="primary" @click="loginIn">登录</el-button>
    </div>
</template>

<script setup>
import { ElMessage } from 'element-plus'
import { useRouter } from "vue-router";
import { login } from "@/api/user";
import { ref, onMounted } from "vue";
const router = useRouter();
const username = ref("hbz");
const password = ref("123465");

// 获取跳转是否带过来了账号密码
const init = () => {
    const query = router.currentRoute.value.query;
    if (query.username) {
        username.value = query.username;
    }
    if (query.password) {
        password.value = query.password;
    }
}

const loginIn = () => {
    login({
        username: username.value,
        password: password.value
    }).then(res => {
        if (res.code == 200) {
            localStorage.setItem("token", res.data.token)
            localStorage.setItem("lifespan", new Date().getTime())
            ElMessage.success("登录成功")
            router.push("/")
        }
    })
}

onMounted(() => {
    init();
});
</script>